package com.neusoft.elm.dao.impl;

import com.neusoft.elm.dao.CartDao;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.po.Cart;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    private Connection conn=null;
    private PreparedStatement prep=null;
    private ResultSet rs=null;

    @Override
    public List<Cart> listCart(Cart cart) throws Exception {

        try {
            List<Cart>list=new ArrayList<>();
            conn=DBUtil.getConnection();
            List params=new ArrayList();
            StringBuilder sb=new StringBuilder();
            sb.append(" SELECT c.*,f.*,b.* ");
            sb.append(" FROM cart c LEFT JOIN food f ");
            sb.append(" ON c.foodId=f.foodId ");
            sb.append(" LEFT JOIN business b ");
            sb.append(" ON c.businessId=b.businessId ");
            sb.append(" where 1=1 ");
            if (cart.getFoodId()!=null){
                sb.append(" and c.foodId=? ");
                params.add(cart.getFoodId());
            }
            if(cart.getBusinessId()!=null){
                sb.append(" and c.businessId=? ");
                params.add(cart.getBusinessId());
            }
            if(cart.getUserId()!=null){
                sb.append(" and c.userId=? ");
                params.add(cart.getUserId());
            }
            System.out.println(sb.toString());//打印SQL
            prep = conn.prepareStatement(sb.toString());
            //设置参数
            for (int i = 0; i < params.size(); i++) {
                prep.setObject(i+1,params.get(i));
            }
            rs = prep.executeQuery();
            while(rs.next()) {
                Cart c = new Cart();
                c.setCartId(rs.getInt("cartId"));
                c.setBusinessId(rs.getInt("businessId"));
                c.setFoodId(rs.getInt("foodId"));
                c.setQuantity(rs.getInt("quantity"));
                c.setUserId(rs.getString("userId"));
                Food food = new Food();
                food.setBusinessId(rs.getInt("businessId"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setRemarks(rs.getString("remarks"));
               c.setFood(food);
                Business bussiness = new Business();
                bussiness.setBusinessId(rs.getInt("businessId"));
                bussiness.setBusinessName(rs.getString("businessName"));
                bussiness.setBusinessImg(rs.getString("businessImg"));
                bussiness.setBusinessExplain(rs.getString("businessExplain"));
                bussiness.setOrderTypeId(rs.getInt("orderTypeId"));
                bussiness.setRemarks(rs.getString("remarks"));
                bussiness.setStarPrice(rs.getDouble("starPrice"));
                bussiness.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                bussiness.setBusinessAddress(rs.getString("businessAddress"));
                c.setBusiness(bussiness);
                list.add(c);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(rs,prep);
        }
    }
    @Override
    public int saveCart(Cart cart) throws Exception{
int result=0;
String sql="insert into cart values(null,?,?,?,1)";
        try {
            conn=DBUtil.getConnection();
            prep=conn.prepareStatement(sql);
            prep.setInt(1,cart.getFoodId());
            prep.setInt(2,cart.getBusinessId());
            prep.setString(3,cart.getUserId());
            result=prep.executeUpdate();
        } finally {
            DBUtil.close(prep);
        }
        return result;
    }
    @Override
    public int removeCart(Cart cart) throws Exception{
        int result= 0;
        try {
            List params=new ArrayList();
            result = 0;
            StringBuilder sql=new StringBuilder("delete from cart where 1=1");
            if (cart.getFoodId()!=null){
                sql.append("and foodId =?"+cart.getFoodId());
                params.add(cart.getFoodId());
            }
            if (cart.getBusinessId()!=null){
                sql.append("and businessId=?"+cart.getBusinessId());
                params.add(cart.getBusinessId());
            }
            if (cart.getUserId()!=null){
                sql.append("and userId=?"+cart.getUserId());
                params.add(cart.getUserId());
            }
            System.out.println(sql.toString());
            prep=conn.prepareStatement(sql.toString());
            for (int i = 0; i <params.size() ; i++) {
                prep.setObject(i+1,params.get(i));
            }


            conn=DBUtil.getConnection();
            prep=conn.prepareStatement(sql.toString());
            result=prep.executeUpdate();
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            DBUtil.close(prep);
        }


    }
        @Override
    public int updateCart(Cart cart) throws Exception{
            int result = 0;
            String sql = "update cart set quantity=? where foodId=? and businessId=? and userId=?";
            try {
                conn = DBUtil.getConnection();
                prep = conn.prepareStatement(sql);
                prep.setInt(1, cart.getQuantity());
                prep.setInt(2, cart.getFoodId());
                prep.setInt(3, cart.getBusinessId());
                prep.setString(4, cart.getUserId());
                result = prep.executeUpdate();
            }finally {
                DBUtil.close(prep);
            }
            return result;
        }
        }



